Walkthrough 8-1: Consume a RESTful web service that has an API (and connector) in Exchange
In this walkthrough, you consume the American flights RESTful web service that you built that has an API and a connector in Exchange. You will:
· Create a new flow to call the American RESTful web service.
· Add a REST connector from Exchange to an Anypoint Studio project.
· Configure and use a REST connector to make a call to a web service.
· Dynamically set a query parameter for a web service call.
Starting file
If you did not complete the previous walkthrough, you can get a starting file here. This file is also located in the solutions folder of the student files ZIP located in the Course Resources.
Review the auto-generated REST connectors in Exchange
1. Return to Exchange in Anypoint Platform.
2. Locate your American flights API.
3. Select the API and review its information.
4. Click the download drop-down menu button; you should see that REST Connect has created connectors for the API in Exchange.
Make a request to the web service
5. Return to Advanced REST Client.
6. Select the first tab and set client_id and client_secret as headers using the values you saved in the course snippets.txt file.
7. Send a GET request to your American Flights API http://training4‑american‑api‑{lastname}.{region}.cloudhub.io/flights; you should still see JSON data for the American flights as a response.
Add a new flow with an HTTP Listener
8. Return to the apdev-flights-ws project in Anypoint Studio.
9. Open implementation.xml
10. Drag out an HTTP Listener and drop it in the canvas.
11. Rename the flow to getAmericanFlights.
12. In the Listener properties view, set the display name to GET /american.
13. Ensure the connector configuration is set to the existing HTTP_Listener_config.
14. Set the path to /american.
15. Set the allowed methods to GET.
Add the American Flights API module to Anypoint Studio
16. In the Mule Palette, select Search in Exchange.
17. In the Add Dependencies to Project dialog box, enter american in the search field.
18. Select your American Flights API (not the Training: American Flights API) and click Add.
Note: If you did not successfully create the American Flights API in the first part of the course, you can use the pre-built Training: American Flights API connector.
19. Click Finish.
20. Wait for the module to be downloaded.
21. Select the new American Flights API module in the Mule Palette.
Add a Get all flights operation
22. Select the Get flights operation in the right side of the Mule Palette and drag and drop it in the process section of the flow.
23. Select the Get flights operation in the canvas and in its properties view, see that you need to create a new configuration and enter a client_id and client_secret.
Configure the American Flights API connector
24. Return to global.xml.
25. In the Global Elements view, click Create.
26. In the Choose Global Type dialog box, select Connector Configuration > American Flights API Config and click OK.
27. In the Global Element Properties dialog box, set each field to a corresponding property placeholder (that you will define and set next):
· host: ${american.host}
· port: ${american.port}
· protocol: ${american.protocol}
· basePath: ${american.basepath}
28. Click OK.
29. Return to the course snippets.txt file and copy the text for the American RESTful web service properties.
30. Return to config.yaml in src/main/resources and paste the code at the end of the file.
31. In the american.host property, replace {lastname} and {region} with your application identifiers.
32. Return to Advanced REST Client and copy the value for your client_id.
33. Return to config.yaml and paste the value within double quotes.
34. Repeat for your client_secret.
Note: If you have used the pre-built Training: American Flights API, use the host, client_id, and client_secret values from the course snippets.txt file.
Configure the Get flights operation
35. Return to implementation.xml.
36. In the Get flights properties view, ensure the Connector configuration is set to American_Flights_API_Config.
37. Set the client id to the ${american.client_id} property.
38. Set the client secret to the ${american.client_secret} property.
39. Leave the destination field blank.
Review metadata associated with the operation
40. Select the output tab in the DataSense Explorer and expand Payload.
Test the application
41. Run the project.
42. In the dialog box, ensure all the resources are selected and click Save Selected.
43. In Advanced REST Client, return to the tab with the localhost request.
44. Change the URL to make a request to http://localhost:8081/american; you should get all the flights.
Review the specification for the API you are building
45. Return to mua-flights-api.raml in src/main/resources/api.
46. Review the optional query parameters.
47. Locate the return type for the get method of the /flights resource.
48. Open FlightsExample.raml in src/main/resources/api.examples and review its structure.
Create a variable to set the destination airport code
49. Return to implementation.xml.
50. Drag a Sub Flow scope from the Mule Palette and drop it at the top of the canvas.
51. Change the name of the flow to setCode.
52. Drag a Set Variable transformer from the Mule Palette and drop it in the setCode subflow.
53. In the Set Variable properties view, set the name and display name to code.
54. Switch the value field to expression mode then set its value to a query parameter called code and give it a default value of SFO if no query parameter is passed to the flow.
message.attributes.queryParams.code default 'SFO'
Dynamically set a query parameter for the web service call
55. Drag a Flow Reference component from the Mule Palette and drop it after the GET /american Listener in getAmericanFlights.
56. In the Flow Reference properties view, set the flow name and display name to setCode.
57. In the Get flights properties view, switch the destination field to expression mode then set its value to the variable containing the airport code.
vars.code
Test the application
58. Save the file to redeploy the application.
59. In Advanced REST Client, send the same request; this time you should only get flights to SFO.
60. Add query parameter called code equal to LAX and make the request; you should now only see flights to LAX.
61. Examine the data structure of the JSON response.
Note: You will transform the data to the format specified by the mua-flights-api in a later walkthrough.